package com.service.device;

import com.dao.secondary.device.DeviceObdRepairRecordInfoMapper;
import com.dao.secondary.device.DevicePcbRepairRecordInfoMapper;
import com.model.domain.device.GetDeviceObdBaseInfoDO;
import com.model.domain.device.GetDevicePcbBaseInfoDO;
import com.model.pojo.device.DeviceObdBaseInfo;
import com.model.pojo.device.DeviceObdRepairRecordInfo;
import com.model.pojo.device.DevicePcbBaseInfo;
import com.model.pojo.device.DevicePcbRepairRecordInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author: zql
 * @Description: OBD设备工厂维修记录服务
 * @Data: 2023-07-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DevicePcbRepairRecordInfoService {

    private final DevicePcbRepairRecordInfoMapper devicePcbRepairRecordInfoMapper;
    private final DevicePcbBaseInfoService devicePcbBaseInfoService;
    @Transactional
    public List<DevicePcbBaseInfo> saveRepairInfo(DevicePcbRepairRecordInfo info){
        GetDevicePcbBaseInfoDO getDevicePcbBaseInfoDO = new GetDevicePcbBaseInfoDO();
        getDevicePcbBaseInfoDO.setImei(info.getImei());
        getDevicePcbBaseInfoDO.setSortRow("id");
        List<DevicePcbBaseInfo> tempList = new ArrayList<>();
        String dtus = "";
        List<DevicePcbBaseInfo> list = devicePcbBaseInfoService.getAllList(getDevicePcbBaseInfoDO);
        if(list!=null && list.size()>0){
            for(DevicePcbBaseInfo baseInfo :list){
                baseInfo.setCurVersion("");
                baseInfo.setLon("");
                baseInfo.setLat("");
                baseInfo.setImei("");
                baseInfo.setSim("");
                baseInfo.setLastModifiedTime(new Date());
                tempList.add(baseInfo);
                dtus+=baseInfo.getDtu()+";";
            }
            devicePcbBaseInfoService.updateList(tempList);
            info.setDtu(dtus);
            info.setCreateTime(new Date());
            devicePcbRepairRecordInfoMapper.insertSelective(info);
        }

        return tempList;
    }
}
